<?php
namespace Home\Controller;
use Think\Controller;
Class IndexController extends Controller
{
    /**
     * 主页面显示 1县  2国家 列表数据
     */
     public function indexs()
    {
        $this->display('media/media_index');
    }

    /**
     * 注册
     */
	public function register()
	{
	    $post = I("post.");

        $info =  $this->validate($post);

        $res = array();

        $res['user_username'] = $post['user_username'];

        $res['user_password'] = MD5($post['user_password']);

        $res['user_email']    = $post['user_email'];

        $res['user_phone']    = $post['user_phone'];
		
        $res['user_token']    = md5(uniqid(rand(),true));;

        $res['user_created_time']    = date("Y-m-d H:i:s",time());

        $res['user_updated_time']    = date("Y-m-d H:i:s",time());

        $register = D("Index")->add_user_info($res);
		
		$info['code'] = 0;

        $info['msg']  = '注册成功';
		
        if (empty($register)) {

			$info['code'] = 1;

            $info['msg']  = '注册失败';        
		}
		$this->api_info($info);
		}

    /**
     * 登陆
     */
	public function login()
	{
        $condition["user_username"] = $username = I(trim("post.user_username"));

        $condition["user_password"] = $password = md5(trim(I("post.user_password")));

        $get_user =  D("Index")->get_user_one_info($condition);

		$info['code'] = 0;

		$info['msg']  = '登陆成功'; 
		
		$info['data']  = $get_user['user_token'];       
        if (!$get_user) {

			$info['code'] = 1;

            $info['msg']  = '用户名或密码错误';        
		}
		$this->api_info($info);		
    }

    /**
     * 注册统一验证格式数据
     */
    public function validate($post)
    {
        $post['user_username'] = $post['user_username'];
        $post['user_email']    = $post['user_email'];
        $post['user_password'] = $post['user_password'];
        $post['user_phone']    = $post['user_phone'];

        //验证用户名格式
        if ($post['user_username']) {
            if (!preg_match('/^\w{6,12}+$/',$post['user_username'])) {
                $this->error("用户名格式错误!",U("Index/reg"));
				
				$info['code'] = 2;

				$info['msg']  = '用户名格式错误';   

				$this->api_info($info);					
            }
            $condition["user_username"] = $post['user_username'];

            $where = D("Index")->get_user_info($condition);

            if (count($where) > 0) {

				$info['code'] = 3;

				$info['msg']  = '用户名已存在';   

				$this->api_info($info);				            
			}
        } else {
			$info['code'] = 4;

			$info['msg']  = '用户名不能为空';   

			$this->api_info($info);				        
		}
        //验证邮箱格式
        if ($post['user_email']) {
            if (!preg_match('/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,})$/', $post['user_email'])) {				
				$info['code'] = 5;

				$info['msg']  = '邮箱格式错误';   

				$this->api_info($info);								
            }
        }
        //验证密码格式
        if ($post['user_password']) {
            if (!preg_match('/^[0-9A-Za-z]{6,20}$/',$post['user_password'])) {
				$info['code'] = 6;

				$info['msg']  = '密码格式错误';   

				$this->api_info($info);								
            }
        } else {			
			$info['code'] = 7;

			$info['msg']  = '密码不能为空';   

			$this->api_info($info);				
        }
        //验证手机号格式
        if ($post['user_phone']) {
            if (!preg_match('/^0?(13|15|17|18|14)[0-9]{9}$/i', $post['user_phone'])) {
			$info['code'] = 8;

			$info['msg']  = '手机号格式错误';   

			$this->api_info($info);		
            }
        }
    }

    //验证用户名接口
    public function api_username()
    {
        $condition["user_username"] = I("post.user_username");

        if (!preg_match('/^\w{6,12}+$/',$condition["user_username"])) {
            $info['code'] = 2;

            $info['msg']  = '用户名格式错误';

            $this->api_info($info);
        }
        $get_user_info = D("Index")->get_user_info($condition);

        $info = array();

        $info['code'] = 0;

        $info['msg']  = '用户名可以注册';

        $info['data'] = '';
        if (count($get_user_info) > 0) {

            $info['code'] = 1;

            $info['msg']  = '用户名已存在';
        }
            $this->api_info($info);
    }

    //忘记密码验证手机号
    public function api_phone()
    {

        $condition["user_phone"] = '15910951720';

        $get_user_info = D("Index")->get_user_info($condition);

        if (count($get_user_info) > 0) {

            $info['code'] = 0;

            $info['msg']  = '手机号正确';

            if (!preg_match('/^0?(13|15|17|18|14)[0-9]{9}$/i', '15910951720')) {
                $info['code'] = 1;

                $info['msg'] = '手机号格式错误';
            }
        } else {
            $info['code'] = 2;

            $info['msg']  = '手机号错误';
        }
        $this->api_info($info);
    }

	
	 //忘记密码验证短信验证码
    public function api_code()
    {

        $condition["user_phone"] = '15910951720';
		
        $captcha = trim(I("post.captcha"));

        $get_user_smslog   = D("Index")->get_user_smslog($condition);

		$info['code'] = 0;

        $info['msg'] = '验证码正确';
		
		if ($get_user_smslog['sms_code'] != $captcha) {
			$info['code'] = '1';

            $info['msg'] = '验证码错误';
		}

        $this->api_info($info);
    }
	
	
    //统一返回接口数据
    public function api_info($info = '')
    {
        $validate = array();

        $validate['code'] = $info['code'];

        $validate['msg']  = $info['msg'];

        $validate['data'] = $info['date'];

        echo json_encode($info);die;
    }

    //上传店铺头像
    public function upload_picture()
    {

        $token = trim(I("post.token"));

        $user_token['user_token'] = $token;

        $get_user_info = D("Index")->get_user_one_info($user_token);

        if (empty($get_user_info)) {
            $info['code'] = '3';

            $info['msg']  = '请登录!';

            $this->api_info($info);
        }
        $data = array();

        $data = I("post.");

        $date['user_username']  = trim($data['name']);
        //$date['user_phone']     = trim($data['phone']);
        $date['user_email']     = trim($data['email']);
        //$date['user_password']  = trim($data['password']);
        $date['user_area']      = trim($data['address']);
        $date['user_shop']      = trim($data['shopname']);
        $date['user_picture_path']      = trim($data['shoplogo']);
        $date['user_updated_time']      = date("Y-m-d H:i:s",time());

        $upload = new \Think\Upload();
        $upload->maxSize   =     3145728 ;
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','bmp');
        $upload->rootPath  = "./";
        $upload->savePath  =      './Public/img/shopmall/';
      
        $info_upload   =   $upload->upload();

        $picture_info  = $info_upload["picture"]["savepath"].$info_upload["picture"]["savename"];

        $upload_images = substr($picture_info,9);
        
        $date["user_picture_path"] = $upload_images;

        $where["user_id"] = $get_user_info['user_id'];

        if (!$info_upload) {
            unset($date["user_picture_path"]);
        }

        $res = M("section_user")->where($where)->save($date);

        if($res)
        {
            $info['code'] = '0';

            $info['msg']  = '修改成功!';
        }
        else
        {
            $info['code'] = '1';

            $info['msg']  = '修改失败!';
        }
        $this->api_info($info);
    }

    //token过期自动跳转至登陆页面
    public function redirect_login()
    {
        $this->display("Index/login");
    }

    //全站搜索
    public function search()
    {
        $goods_name = trim(I("post.search"));

        $condition['goods_status'] = 1;

        $condition['goods_state']  = 1;

        $list = M("section_goods")->where("goods_name like  '%$goods_name%' and goods_status = 1 and goods_state = 1")->select();
				
		$info['code'] = '1';

        $info['msg']  = '暂无数据';
		if ($list) {
		
			foreach($list as $k=>$v){
				$list[$k]['goods_name'] = str_replace($goods_name,"<font color='red'>$goods_name</font>",$v['goods_name']);
			}
			$info['code'] = '0';

			$info['msg']  = '搜索成功';
		
			$info['data'] = $list;
		}
        $this->api_info($info);
    }

      //首页显示最新发布信息
    public function new_goods()
    {
        $condition['goods_state'] = "1";
		
        $condition['goods_type']  = "1";

        $new_goods    = D("Goods")->get_goods_info($condition);

        $info['code'] = '0';

        $info['msg']  = '成功';

        $info['data'] = $new_goods;
        if (!$new_goods) {
            $info['code'] = '1';

            $info['msg']  = '暂无数据!';

            $info['data'] = $new_goods;
        }
        $this->api_info($info);
    }
	
	//发送验证短信
	public function _sendsms()
	{
		$apikey = '69ec50747154e4f191b2f72c341e502f';
		
		$condition['sms_phone'] = $mobile = '15910951117';//trim(I("post.user_phone"));
		
		$info['code'] = 0;
			
		$info['msg']  = "发送成功";
			
		if (!$mobile) {
			$info['code'] = 1;
			
			$info['msg']  = "手机号不准为空";
			
			$this->api_info($info);die;

		}
		$verify_code = rand();

		$get_user_sms = D("Index")->get_user_smslog($condition);
		
		if ($get_user_sms) {

			if ($get_user_sms['sms_created_time'] > time() - 60) {
				$info['code'] = 2;
			
				$info['msg']  = "一分钟内禁止重复发送验证码！";
				
				$this->api_info($info);die;
			}
		}
		
		$text = '【项目节】您的验证码是'.$verify_code.'如非本人操作请忽略本短信';
		
		$url = "http://yunpian.com/v1/sms/send.json";

		$encoded_text = urlencode("$text");

		$post_string = "apikey=$apikey&text=$encoded_text&mobile=$mobile";

		$result = $this->sock_post($url,$post_string);
		
		$res['sms_phone']		 = $mobile;
		
		$res['sms_code']		 = $verify_code;
		
		$res['sms_created_time'] = time();
		
		$res['sms_updated_time'] = time();
		
		M("section_code_log")->where($condition)->delete();
		
		M("section_code_log")->add($res);
		
		if ($result != 0 ) {
			$info['code'] = 3;
			
			$info['msg']  = "发送失败";
			
		}
		$this->api_info($info);
	}

	/**

	* url 为服务的url地址

	* query 为请求串

	*/

	public function sock_post($url,$query)
	{

		$data = "";

		$info=parse_url($url);

		$fp=fsockopen($info["host"],80,$errno,$errstr,30);

		if(!$fp){

			return $data;

		}

		$head="POST ".$info['path']." HTTP/1.0\r\n";

		$head.="Host: ".$info['host']."\r\n";

		$head.="Referer: http://".$info['host'].$info['path']."\r\n";

		$head.="Content-type: application/x-www-form-urlencoded\r\n";

		$head.="Content-Length: ".strlen(trim($query))."\r\n";

		$head.="\r\n";

		$head.=trim($query);

		$write=fputs($fp,$head);

		$header = "";

		while ($str = trim(fgets($fp,4096))) {

			$header.=$str;

		}

		while (!feof($fp)) {

			$data .= fgets($fp,4096);

		}

		return $data;

	}
	
	//个人中心
	public function personal()
	{
		
		$condition['user_token'] = trim(I("post.token"));
		
		$condition['user_state'] = 1;
		
		$get_user = D("Index")->get_user_one_info($condition);

		if (!$get_user) {
            $info['code']  = 2;

            $info['msg']   = "请登录";

            $this->api_info($info);
        }
		$info['code']  = 0;
			
		$info['msg']   = "成功";
		
		$info['data']  = $get_user;

		if (empty($get_user)) {
			$info['code'] = 1;
			
			$info['msg']  = "失败";
		}

		$this->api_info($info);
	}
	
	//开通店铺
	public function up_shop()
	{
		$token = trim(I("post.token"));

        $user_token['user_token'] = $token;

        $get_user_info = D("Index")->get_user_one_info($user_token);

        if (empty($get_user_info)) {
            $info['code'] = 2;

            $info['msg']  = '请登录!';

            $this->api_info($info);
        }
        $data = array();

        $data = I("post.");

        $date['user_open'] 		   = 1;
        $date['user_shop'] 		   = trim($data['user_shop']);
        $date['user_shop_area']    = trim($data['user_shop_area']);
        $date['user_shop_path']    = trim($data['user_shop_path']);
        $date['user_cc_id']		   = intval($data['user_cc_id']);
        $date['user_shop_category']= trim($data['user_shop_category']);
        $date['user_created_time'] = date("Y-m-d H:i:s",time());
        $date['user_updated_time'] = date("Y-m-d H:i:s",time());

        $upload = new \Think\Upload();
        $upload->maxSize   =     3145728 ;
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','bmp');
        $upload->rootPath  = "./";
        $upload->savePath  =      './Public/img/shop/';
      
        $info_upload   =   $upload->upload();

        $picture_info  = $info_upload["picture"]["savepath"].$info_upload["picture"]["savename"];
		
		if ($picture_info) {
			$upload_images = substr($picture_info,9);
        
			$date["user_shop_path"] = $upload_images;
		}
        $where["user_id"] = $get_user_info['user_id'];

        $res = M("section_user")->where($where)->save($date);

        if($res)
        {
            $info['code'] = 0;

            $info['msg']  = '成功!';
        }
        else
        {
            $info['code'] = 1;

            $info['msg']  = '失败!';
        }
        $this->api_info($info);		
	}
		
}
?>
